<template>
  <div class="app-container">
    <!--搜索栏-->
    <el-form :model="queryParams" ref="queryRef" :inline="true" label-width="68px">
      <el-form-item label="关键字搜索" label-width="85" prop="keyValue">
        <el-input
          v-model="queryParams.keyValue"
          placeholder="请输入搜索关键字"
          clearable
          style="width: 240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!--操作栏-->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['hidden:user:add']"
          >新增</el-button
        >
      </el-col>
    </el-row>
    <!--数据列表-->
    <el-table v-loading="loading" :data="dataList">
      <el-table-column type="index" label="序号" width="55" align="center" />
      <el-table-column label="所属部门" prop="deptName" :show-overflow-tooltip="true" />
      <el-table-column label="人员姓名" prop="userName" :show-overflow-tooltip="true" />

      <el-table-column label="创建时间" prop="createdAt">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createdAt) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建人" prop="createdBy" />
      <el-table-column
        label="操作"
        align="center"
        width="160"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-tooltip content="编辑" placement="top">
            <el-button
              link
              type="primary"
              icon="Edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['hidden:user:edit']"
            ></el-button>
          </el-tooltip>
          <el-tooltip content="删除" placement="top">
            <el-button
              link
              type="primary"
              icon="Delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['hidden:user:remove']"
            ></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.current"
      v-model:limit="queryParams.size"
      @pagination="getList"
    />
    <!--新增对话框-->
    <el-dialog :title="title" v-model="open" width="680px" append-to-body>
      <el-form ref="formRef" :model="form" :rules="rules">
        <el-row>
          <el-col :span="24">
            <el-form-item label="隐患分配人:" label-width="140px">
              <el-select v-model="form.userId" clearable filterable placeholder="请选择">
                <el-option
                  v-for="dict in userListOptions"
                  :key="dict.id"
                  :label="dict.label"
                  :value="dict.id"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
<script setup name="Allocation">
import {
  listAllocation,
  addAllocation,
  deleteAllocation,
  getAllocation,
  updateAllocation,
} from "@/api/prevent/hidden/allocation";
import { getAllUserList } from "@/api/system/user";

const title = ref(""); //对话框标题

const { proxy } = getCurrentInstance();
const open = ref(false);
const total = ref(0);
const loading = ref(false);
const dataList = ref([]); //数据列表
const userListOptions = ref([]); //获取用户列表

const data = reactive({
  form: {},
  queryParams: {
    current: 1,
    size: 10,
    keyValue: undefined,
    riskLevelId: undefined,
    riskUnitId: undefined,
    determineMethodType: undefined,
  },
  rules: {},
});
const { queryParams, form, rules } = toRefs(data);

/** 获取数据列表 */
function getList() {
  listAllocation(queryParams.value).then((res) => {
    dataList.value = res.data.dataList;
    total.value = res.data.totalNum;
  });
}

/** 处理搜索 */
function handleQuery() {
  getList();
}
/** 重置 */
function resetQuery() {
  queryParams.value = {
    current: 1,
    size: 10,
  };
}
/** 处理新增 */
function handleAdd() {
  title.value = "添加隐患分配人";
  open.value = true;
}
/** 处理编辑 */
function handleUpdate(row) {
  getAllocation(row.hiddenUserId).then((res) => {
    open.value = true;
    title.value = "编辑隐患分配人";
    form.value = res.data;
  });
}
/** 处理删除 */
function handleDelete(row) {
  proxy.$modal
    .confirm('是否确认删除名称为"' + row.userName + '"的数据项?')
    .then(function () {
      return deleteAllocation(row.hiddenUserId);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}
/** 数据提交 */
function submitForm() {
  if (form.value.userId != "" && form.value.userId != undefined) {
    if (form.value.hiddenUserId != undefined) {
      updateAllocation(form.value).then((response) => {
        proxy.$modal.msgSuccess("修改隐患分配人成功");
        open.value = false;
        getList();
      });
    } else {
      addAllocation(form.value).then((response) => {
        proxy.$modal.msgSuccess("添加成功！");
        open.value = false;
        getList();
      });
    }
  }
}
/** 取消 */
function cancel() {
  open.value = false;
}

function getUserOptions() {
  getAllUserList().then((res) => {
    userListOptions.value = res.data;
  });
}
getList();
getUserOptions();
</script>
